/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.pacs.services;

import akka.actor.ActorRef;
import com.cxhd.pacs.DataConfig;
import com.cxhd.pacs.actor.ActorManager;
import com.cxhd.pacs.model.FileDetail;
import com.cxhd.pacs.model.HostDetail;
import com.cxhd.pacs.model.HostInfo;
import com.cxhd.pacs.tools.DateUtil;
import com.cxhd.pacs.tools.FileUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jfinal.core.Controller;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 52461
 */
public class HostService {

    HostDetail hd = new HostDetail();
 private ActorRef arf = ActorManager.getIntance().getActor("savePatInfoActor");
    public void parsreJsonAndSave(File[] list, String fileSystemId,Controller con) throws Exception {

        for (File file : list) {
            System.out.println(file.getName().endsWith("json".toUpperCase()));
            if (file.getName().endsWith("json".toUpperCase())) {
                getJsonData(FileUtil.outWriter(file.getAbsolutePath(),"GBK"), list, fileSystemId,con);
                break;
            }
        }

    }

    public void getJsonData(String info, File[] list, String fileSystemId,Controller con) throws Exception {
        System.out.println(info);
        Map<String, String> map = new Gson().fromJson(info, new TypeToken<Map<String, String>>() {
        }.getType());
        String hospitalCode =con.getPara("hospitalCode");
         if (hospitalCode == null || !hospitalCode.startsWith("CX0712131")) {
                hospitalCode = DataConfig.getInstance().getConfig().get("centerHospitalCode");
            }
        Map<String, String> newMap = new HashMap<String, String>();
        newMap.put("name", map.get("name"));
        newMap.put("age", map.get("age"));
        newMap.put("sex", map.get("sex"));
        newMap.put("phone", map.get("phone"));
        newMap.put("address", map.get("address"));
        newMap.put("card_no", map.get("card_no"));
        newMap.put("birth", map.get("birth"));
        newMap.put("info_id", map.get("patient_no"));
       // System.out.println(""+con.getPara("hospitalCode"));
       // System.out.println(map.get("hospital")==null?con.getPara("hospitalCode"): map.get("hospital")+"========================!!!!!!!!!!!!");
        newMap.put("hospital_code", hospitalCode);
        String hostId = new HostInfo().getHostId(newMap);
        System.out.println(hostId);
        String content = null;
        String resultId = "";
        for (File file : list) {
            if (file.getName().toLowerCase().endsWith(map.get("type").toLowerCase())) {
                String str = file.getAbsolutePath();
                str = str.substring(str.indexOf("\\", 3) + 1);
                if (resultId.equalsIgnoreCase("")) {
                    resultId = new FileDetail().saveInfo(map.get("type").toLowerCase(), file.getName(), str, file.length() + "", fileSystemId + "");
                } else {
                    resultId += "," + new FileDetail().saveInfo(map.get("type").toLowerCase(), file.getName(), str, file.length() + "", fileSystemId + "");
                }
            } else if (file.getName().toLowerCase().endsWith("txt".toLowerCase())) {
                content = FileUtil.outWriter(file.getAbsolutePath());
            }
            if(file.getName().toLowerCase().endsWith("pdf".toLowerCase())){
                System.out.println("发送转换JPG");
            ActorManager.getIntance().getActor("pdfToJpgActor").tell(file.getAbsolutePath(), ActorRef.noSender());
            }
        }
        Map<String, String> maps = new HashMap<String, String>();

        maps.put("host_id", hostId);
        if(map.get("event_name")==null){
             maps.put("event_name", "病历");
        }else{
             maps.put("event_name", map.get("event_name"));
        }
       
        maps.put("event_description", map.get("event_description"));
        maps.put("event_result", resultId);
        if(map.get("event_time")!=null){
         maps.put("event_time",map.get("event_time"));
        }
        else{
             maps.put("event_time", DateUtil.getDateEN());
        }
        maps.put("info_id", map.get("patient_no"));
       maps.put("hospital_code", map.get("hospital")==null?con.getPara("hospitalCode"): map.get("hospital"));
//        maps.put("hospital_name", DataConfig.getInstance().getHospitalName());
        maps.put("createtime", DateUtil.getDateEN());
        maps.put("result_type", map.get("type").toLowerCase().equalsIgnoreCase("jpg") ? "image" : map.get("type").toLowerCase());
        maps.put("data1", content);
        maps.put("update_id", map.get("update_id"));
        System.out.println(hd.checkUpdateId(map.get("update_id"),hostId));
        if (hd.checkUpdateId(map.get("update_id"),hostId)) {
            System.out.println("-------------------------------保存");
            hd.saveDetail2(maps);
        } else {
            hd.updateDetail(maps);
              System.out.println("-------------------------------更新");
        }
        newMap.putAll(maps);
         arf.tell(newMap, ActorRef.noSender());
      //  new HostDetail().saveDetail(maps);
    }
}
